#include "EventLoop.h"
#include <time.h>
#include <functional>

static int TimeStamp()
{
    struct timespec tp;
    clock_gettime(CLOCK_MONOTONIC, &tp);
    return tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
}

void func1(EventLoop *loop)
{
    static int lastTime;
    int currentTime = TimeStamp();
    printf("%s: %d [%d]\n", __func__, __LINE__, currentTime - lastTime);
    lastTime = currentTime;
    loop->DelayRun(std::bind(func1, loop), 1.1102);
}

void func2(EventLoop *loop)
{
    static int lastTime;
    static int tick = 0;
    int currentTime = TimeStamp();
    printf("%s: %d [%d]\n", __func__, __LINE__, currentTime - lastTime);
    if (++tick >= 100) {
        loop->Stop();
    } else if (tick % 10 == 0) {
        loop->Run(std::bind(func2, loop));
    } else {
        loop->DelayRun(std::bind(func2, loop), 3.1256);
    }
    lastTime = currentTime;
}

int main(int argn, char **argv)
{
    EventLoop loop;
    loop.Run(std::bind(func1, &loop));
    loop.Run(std::bind(func2, &loop));
    loop.Loop();
    return 0;
}
